package com.flyme.module.file.provider.controller;


import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.module.file.client.api.SysCourseTypeRemoteService;
import com.flyme.module.file.client.entity.SysCourse;
import com.flyme.module.file.client.entity.SysCourseType;
import com.flyme.module.file.provider.service.SysCourseService;
import com.flyme.module.file.provider.service.SysCourseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * @author flyme
 */
@Slf4j
@Api(tags = "课程类型管理")
@RestController
public class SysCourseTypeController extends BaseController<SysCourseTypeService, SysCourseType> implements SysCourseTypeRemoteService {

    @Autowired
    private SysCourseService courseService;

    @ApiOperation(value = "获取课程分类")
    @Override
    public ResultBody selectTreeData(@RequestParam(value = "parentId",required =false) String parentId, @RequestParam(value = "title",required=false) String title) {
        CriteriaQuery qw = new CriteriaQuery<Map<String, Object>>();
        qw.select("id value", "courseTypeName label","typeImg", "parentId", "isParent","sortOrder");
        qw.eq("parentId", parentId);
        qw.orderByAsc("sortOrder");
        qw.like("courseTypeName",title);
        List list = bizService.listMaps(qw);
        return ResultBody.success(toCascader(list));
    }

    @ApiOperation(value = "添加课程分类", notes = "添加课程分类")
    @Override
    public ResultBody addCourseType(@ModelAttribute(value = "courseType") SysCourseType courseType) {
        boolean result = bizService.addCourseType(courseType);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "编辑课程分类", notes = "编辑课程分类")
    @Override
    public ResultBody edit(SysCourseType courseType) {
        boolean result= bizService.updateCourseType(courseType);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "删除课程分类", notes = "删除课程分类")
    @Override
    public ResultBody delById(@PathVariable Long coureTypeId) {
        CriteriaQuery cq = new CriteriaQuery<SysCourse>();
        Integer courseNum = courseService.count(cq.eq("courseTypeId", String.valueOf(coureTypeId)));
        if(courseNum > 0){
            return ResultBody.failed("该分类下有课程，请先删除课程");
        }
        return bizService.delById(coureTypeId);
    }

    /**
     * 查询课程所属模块
     */
    @ApiOperation(value = "查询分类列表", notes = "查询分类列表")
    @Override
    public ResultBody getCourseType() {
        List<SysCourseType> list = bizService.selectByParentId(0L);
        for (SysCourseType courseType : list) {
            List<SysCourseType> child = bizService.selectByParentId(courseType.getId());
            courseType.setChild(child);
        }
        return ResultBody.ok("设置成功").setData(list);
    }
}
